//
//  GwqList_Vc.swift
//  Pods
//
//  Created by eme on 2017/4/5.
//
//


/*
 owner:cy
 update:2017年04月05日10:49:47
 info:优惠券列表选择
 */
import RxSwift
import UtilCore
import RickonCore

class GwqList_Vc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    
    @IBOutlet weak var gwqList_Tv: UITableView!
    /*----------------------------   自定义属性区域    ----------------------------*/
    
    var myvoucherlist:[VouchersModel]?
    
    ///缓存选中的优惠券model
    var voucherItem:VouchersModel?
    
    /// 修改状态
    var didUpdateState:((_ item:VouchersModel?) -> Void)?
    /****************************Storyboard 绑定方法区域****************************/
    
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        self.title = "\(AppConfig.appTitle)优惠券"
        ///隐藏滑动条
        self.gwqList_Tv.showsVerticalScrollIndicator = false
    }
    /**
     app 主题 设置
     */
    override func setViewTheme(){
        
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
    }
}
// MARK: UITableViewDelegate
extension GwqList_Vc :UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        if let list = self.myvoucherlist {
            return list.count + 1
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "Gwq_TCell", for: indexPath) as! Gwq_TCell
        if indexPath.row == self.myvoucherlist!.count {
            cell.sperateLine_V.isHidden = true
            cell.showTitle_Lb.text = "不使用优惠券"
            if self.voucherItem == nil {
                cell.state_ImgV.image = UIImage(named: "gwq_selected", in: AryaCore.bundle, compatibleWith: nil)
            }else{
                cell.state_ImgV.image = UIImage(named: "gwq_unselected", in: AryaCore.bundle, compatibleWith: nil)
            }
        }else{
            let item = self.myvoucherlist![indexPath.row]
            cell.sperateLine_V.isHidden = true
            cell.showTitle_Lb.text = item.showTitle
            if let voucherItem = self.voucherItem, voucherItem.uvid == item.uvid{
                cell.state_ImgV.image = UIImage(named: "gwq_selected", in: AryaCore.bundle, compatibleWith: nil)
            }else{
                cell.state_ImgV.image = UIImage(named: "gwq_unselected", in: AryaCore.bundle, compatibleWith: nil)
            }
        }
        return cell
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
}
// MARK: UITableViewDelegate
extension GwqList_Vc: UITableViewDelegate {
    /// 点击行事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.row == self.myvoucherlist?.count {
            if let didUpdateState = self.didUpdateState {
                didUpdateState(nil)
            }
        }else{
            let item = self.myvoucherlist![indexPath.row]
            self.voucherItem = item
            if let didUpdateState = self.didUpdateState {
                didUpdateState(item)
            }
        }
        _ = self.navigationController?.popViewController(animated: true)
        
    }
    
}
